package com.example.student1.webapi;


import com.example.student1.core.PageUtils;
import com.example.student1.dao.ScoreRepository;
import com.example.student1.dao.StudentRepository;
import com.example.student1.domain.Score;
import com.example.student1.domain.Student;
import com.example.student1.domain.Student_Login;
import com.example.student1.domain.Teacher_Login;
import com.example.student1.helper.R;
import com.example.student1.helper.RUtil;
import com.example.student1.service.ScoreService;
import com.example.student1.service.StudentService;
import com.example.student1.service.Student_LoginService;
import com.example.student1.service.Teacher_LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.util.ArrayList;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ScoreService scoreService;

    /*
    *read url:/webapi/list
    */
    @GetMapping("/list")
    public List<Student> list(String name){
        List<Student> students = studentService.findAll();
        return students;
    }


    @GetMapping("/findbyname")
    public List<Student> findbyname(String name){
        List<Student> students = new ArrayList<>();
        if(name == null || name == "")
        {
            students = studentService.findAll();
        }
        else{
            students = studentService.findbyname("%"+name+"%");
        }
        return students;
    }


    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                       @RequestParam(value = "size",defaultValue = "10") Integer size,
                       @RequestParam(value = "name",defaultValue = "") String name,
                       @RequestParam(value = "score",defaultValue = "") Integer score,
                       @RequestParam(value = "sex",defaultValue = "") Integer sex)
    {

        Sort sort = Sort.by(Sort.Direction.DESC,"id");

        Page<Student> studentPage;

        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(score) && StringUtils.isEmpty(sex)){
            Pageable pageable = PageRequest.of(page, size, sort);

            studentPage = studentService.findAll(pageable);
        }
        else{
            Student student = new Student();
            student.setName(name);
            student.setSex(sex);
            student.setScore(score);

            Pageable pageable = PageRequest.of(page, size, sort);

            ExampleMatcher matcher= ExampleMatcher.matching().withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains()).withMatcher("score",ExampleMatcher.GenericPropertyMatchers.contains()).withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.contains());


            Example<Student> examplename = Example.of(student,matcher);


            studentPage = studentService.findbyexample(examplename,pageable);
        }

        //PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        //return pageUtils;
       return RUtil.success(studentPage);
    }

/*    @RequestMapping("/index")
    public class IndexController {

        @Autowired
        HttpServletRequest request; //通过注解获取一个request

        @RequestMapping("/getName")
        public String getName() {
            //通过request来获取保存在session中的用户名
            String userName = (String) request.getSession().getAttribute("userName");
            System.out.println(userName);
            return userName;
        }
    }*/

    @GetMapping("/getbypage1")
    public R getByPage1(HttpServletRequest request,
                        @RequestParam(value = "page",defaultValue = "0") Integer page,
                        @RequestParam(value = "size",defaultValue = "10") Integer size)
                       /*@RequestParam(value = "name",defaultValue = "") String name)*/
    {

        String name = (String) request.getSession().getAttribute("username");

        Sort sort = Sort.by(Sort.Direction.DESC,"id");

        Page<Score> scorePage;

        /*if (StringUtils.isEmpty(name)){
            Pageable pageable = PageRequest.of(page, size, sort);

            studentPage = studentService.findAll(pageable);
        }*/
            Pageable pageable = PageRequest.of(page, size, sort);

            scorePage = scoreService.findBySname(name,pageable);


        //PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        //return pageUtils;
        return RUtil.success(scorePage);
    }

    /*
    * 新增学生信息*"/webapi/student/insert"*/
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Student insert(Student student){

        Student student1 =studentService.insert(student);
        return student1;
    }
    /*
     * 更新学生信息*"/webapi/student/update"*/
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Student update(Student student){

        Student oldstudent1 = studentService.getById(student.getId());
        
        if(StringUtils.isEmpty(student.getSno()))
        {
            student.setSno(oldstudent1.getSno());
        }
        
        student =studentService.update(student);
        return student;
    }

    /*
     * 删除学生信息*"/webapi/student/delete"*/
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public void delete(@PathVariable Long id){
        studentService.deletbyid(id);
    }
    /*
     * 获取学生信息*"/webapi/student/get/id"*/
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Long id){

        Student student = studentService.getById(id);

        student.setSno("");
        log.info("id:{}",id);
        return student;
    }
}
